package generator;

import java.util.Random;

import utilities.Percentage;
import utilities.WeightedRandom;

public class StringGenerator {
	
	public StringGenerator(){
		consonantR = new WeightedRandom(1.0, 4.5, 3.7, 0.9, 1.6, 1.3, 0.1, 0.5, 6.5, 2.5, 6.8, 3.0, 0.3, 6.3, 5.0, 5.6, 2.0, 0.2, 0.4, 0.4);
									//	b	 c	  d    f	g	 h	  j	   k	l	 m    n	   p	q	 r	  s    t	v	 w	  x    z
		vocalR = new WeightedRandom(18.0, 20.0, 17.2, 14.5, 0.8, 4.5);
								//	 a	   e   	 i	   o	 y    u
		lengthR = new WeightedRandom(7.0, 30.0, 40.0, 15.0, 8.0);
								//	 1	  2		3	  4		5
		perc = new Percentage();
	}
	
	public String next(){
		String s = "";
		int length= lengthR.next()+1;
		for(int i= 0; i<length; i++){
			//consonants
			char c = '?';
			boolean hasConsonant = perc.nextRate(80);
			if(hasConsonant || length==1){
				c = consonant.charAt(consonantR.next());
				s += c;
				boolean otherConsonant = perc.nextRate(45);
				if(otherConsonant){
					switch(c){
					case 'p': 
						s+= consonantAfterP.charAt(new Random().nextInt(consonantAfterP.length()));
						break;
					case 'r':
						s+= consonantAfterR.charAt(new Random().nextInt(consonantAfterR.length()));
						break;
					case 's':
						s+= consonantAfterS.charAt(new Random().nextInt(consonantAfterS.length()));
						break;
					default:
						break;
					}
				}else if(doublesConsonant.indexOf(c+"") != -1 && i != 0){
					boolean doubles = perc.nextRate(35);
					if(doubles)
						s += c;
				}
			}
			//vocals
			if(c == 'q')
				s += 'u';
			else if(i == (length-1) && length>1){
				boolean hasLastVocal = perc.nextRate(60);
				if(hasLastVocal)
					s += vocal.charAt(vocalR.next());
			}else
				s += vocal.charAt(vocalR.next());
		}
		return s;
	}
	
	
	
	private WeightedRandom consonantR, vocalR, lengthR;
	private Percentage perc;
	private static final String consonant = "bcdfghjklmnpqrstvwxz";
	private static final String doublesConsonant = "bcdfglmnprstvz";
	private static final String consonantAfterR = "cdmnpst", consonantAfterS="bcfhlmprtv", consonantAfterP = "lrs";
	private static final String vocal = "aeioyu";
}
